package org.fastsyncer.connector.ws;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;

public class WebServiceConnector implements WebService {

	private static WebServiceConnector instance = null;

	private WebServiceConnector() {
	}

	/**
	 * 建议使用该方法
	 * 
	 * @return WebServiceConnector
	 */
	public static WebServiceConnector getInstance() {
		if (instance == null) {
			synchronized (WebServiceConnector.class) {
				instance = new WebServiceConnector();
			}
		}
		return instance;
	}
	
	@Override
	public Object send(String url, String method, String msg) throws Exception {
		// 1.获取一个工厂实例
		JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance();
		
		// 2.生成针对指定服务接口URL的客户端
		Client client = factory.createClient(url);
		
		// 3.调用指定的方法，注意入参第一个为方法名称，第二个为方法的参数（可以传入一个参数列表，一般为数组）
		Object[] objs = client.invoke(method, msg);
		
		// 4.关闭客户端连接
		client.destroy();
		
		// 5.返回响应结果
		return objs[0].toString();
	}
	
}
